import React from 'react';
import {Button, Form, Input, message, Space} from "antd";
import {editUserPasswordUsingPOST, getCurrentUserUsingPOST} from "@/services/model/commitUserController";
import {useModel} from '@@/exports';

const Index = () => {
  const [editForm] = Form.useForm();
  const { initialState } = useModel('@@initialState');
  const onFinishEdit = (values: any) => {
    const bodyParam: API.EditUserPasswordQuery = {
      ...values,
      currentUserId: initialState?.currentUser?.id,
    };
    editUserPasswordUsingPOST(bodyParam).then(res => {
      if (res.code === 0 && res.data) {
        alert("修改成功，请重新登陆")
        getCurrentUserUsingPOST();
      } else {
        message.warning(res.message)
      }

    })
  }
  const onReset = () => {
    editForm.resetFields();
  };
  const layout = {
    labelCol: { span: 8 },
    wrapperCol: { span: 16 },
  };
  return (
    <Form
      {...layout}
      name="basic"
      form={editForm}
      style={{maxWidth: 500}}
      onFinish={onFinishEdit}
    >
      <Form.Item
        label="用户名"
        name="username"
        key="username"
        rules={[{required: true, message: '请输入用户名'}]}
      >
        <Input/>
      </Form.Item>

      <Form.Item
        label="初始密码"
        name="password"
        key="password"
        rules={[{required: true, message: '请输入初始密码'}]}
      >
        <Input type="password"/>
      </Form.Item>

      <Form.Item
        label="新密码"
        name="newPassword"
        key="newPassword"
        rules={[{required: true, message: '请输入新密码'}]}
      >
        <Input type="password"/>
      </Form.Item>
      <Form.Item
        label="确认密码"
        name="checkPassword"
        key="checkPassword"
        rules={[{required: true, message: '请输入新密码'}]}
      >
        <Input type="password"/>
      </Form.Item>
      <Form.Item key="submit&reset" label=" ">
          <Space>
            <Button type="primary" htmlType="submit" key="submit">
              提交
            </Button>
            <Button htmlType="button" onClick={onReset} key="reset">
              重置
            </Button>
          </Space>
      </Form.Item>
    </Form>
  );
};

export default Index;
